package com.CMS.client.ui.widgets;



import com.CMS.client.ui.data.NavigationPaneRecord;

import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;


public class NavigationPane extends VLayout {
	
	  private static final int WEST_WIDTH = 230;
	  
	  private SectionStack sectionStack ;
		
	  public NavigationPane() {
		super();
					
		
	    // initialise the Navigation Pane layout container
		this.addStyleName("cms-NavigationPane");	
	    this.setWidth(WEST_WIDTH);
	    // this.setShowResizeBar(true); 
		
		// initialise the Section Stack
	    sectionStack = new SectionStack();
	    sectionStack.setWidth(WEST_WIDTH);
	    sectionStack.setVisibilityMode(VisibilityMode.MUTEX);
	    sectionStack.setShowExpandControls(true);
	    sectionStack.setAnimateSections(true);	
	    
	    // add the Section Stack to the Navigation Pane layout container
		this.addMember(sectionStack); 
	  }	
	  
	  public void add(String sectionName, NavigationPaneRecord[] sectionData, 
			  		  RecordClickHandler clickHandler) {
		sectionStack.addSection(new NavigationPaneSection(sectionName, sectionData,
								clickHandler));
	  } 

	  public void expandSection(int section) {
		sectionStack.expandSection(section);
	  } 
	  
	  public void selectRecord(String name) {


		    SectionStackSection[] sections = sectionStack.getSections();

		    // Log.debug("Number of sections: " + sections.length);

		    for (int i = 0; i < sections.length; i++) {
		      SectionStackSection sectionStackSection = sections[i];

		      if (((NavigationPaneSection) sectionStackSection).getRecord(name) != -1) {

		        if (!sectionStack.sectionIsExpanded(i)) {

		          sectionStack.expandSection(i);
		        }

		        ((NavigationPaneSection) sectionStackSection).selectRecord(name);
		        break;
		      }
		    }
		  }
}
